#include "utils.h"

bool createNumFigurePatterns(int rows, int cols, int N, double A, double B, double freq, char HV, string filePath)
{
	const double PI = 3.14159265358979323846;
	double T = 1 / freq;
	VectorXd delta = VectorXd::LinSpaced(N, 0, N - 1) * 2 * PI / N;
	VectorXd cVec;
	VectorXd rVec;
	if (HV == 'V')
	{
		cVec = VectorXd::LinSpaced(cols, 1, cols);
		rVec = VectorXd::Ones(rows);
	}
	else if (HV == 'H')
	{
		rVec = VectorXd::LinSpaced(rows, 1, rows);
		cVec = VectorXd::Ones(cols);
	}
	else
	{
		cout << "请输入正确的HV变量(H/V)" << endl;
		return false;
	}
	MatrixXd Phi = (2 * PI / T / cols) * (rVec * cVec.transpose());
	cv::Mat cvMat(rows, cols, CV_8UC1);
	MatrixXd I(rows, cols);
	for (int i = 0; i < N; i++)
	{
		I = A + B * (Phi.array() + delta(i)).cos();
		// Normalize to 0 - 255
		I = 255.0 * (I.array() - I.minCoeff()) / (I.maxCoeff() - I.minCoeff());
		// Convert to OpenCV matrix
		cv::eigen2cv(I, cvMat);
		// Write to .bmp file
		cv::imwrite(filePath + "/" + HV + to_string(i + 1) + ".bmp", cvMat);
	}
	return 1;
}

bool createSingleFigurePattern(int rows, int cols, double A, double B, double freq, char HV, string filePath)
{
	cv::Mat cvMat(rows, cols, CV_8UC1);
	const double PI = 3.14159265358979323846;
	double T = 1 / freq;

	VectorXd cVec;
	VectorXd rVec;
	if (HV == 'V')
	{
		cVec = VectorXd::LinSpaced(cols, 1, cols);
		rVec = VectorXd::Ones(rows);
	}
	else if (HV == 'H')
	{
		rVec = VectorXd::LinSpaced(rows, 1, rows);
		cVec = VectorXd::Ones(cols);
	}
	else
	{
		cout << "请输入正确的HV变量(H/V)" << endl;
		return false;
	}
	MatrixXd Phi = (2 * PI / T / cols) * (rVec * cVec.transpose());
	MatrixXd I = A + B * (Phi.array().cos());
	// Normalize to 0 - 255
	I = 255.0 * (I.array() - I.minCoeff()) / (I.maxCoeff() - I.minCoeff());
	// Convert to OpenCV matrix
	cv::eigen2cv(I, cvMat);
	// Write to .bmp file
	cv::imwrite(filePath + "/" + HV + ".bmp", cvMat);
	return 1;
}
